import { HttpRequestException } from "src/models/http-request-exception";
import { IHttpError } from "src/models/http-error-response";
import * as Joi from '@hapi/joi';
import { HttpStatus } from "@nestjs/common";

export function throwJoiException (joiResult: Joi.ValidationResult) {
  const joiError = joiResult.error || joiResult.errors;
  if (!joiError) {
    return;
  }
  const httpErrors: IHttpError[] = joiError.details.map(o => ({
    field: o.context.key,
    message: o.message,
    code: o.type,
  }));
  throw new HttpRequestException(httpErrors, HttpStatus.BAD_REQUEST);
}

export function throwValidateException (httpErrors: IHttpError[]) {
  throw new HttpRequestException(httpErrors, HttpStatus.BAD_REQUEST);
}
